package com.wenhai.spring.application.context.lifecycle;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.support.GenericApplicationContext;

import java.io.IOException;

/**
 * Spring Shutdown Hook 达到优雅下线
 *
 * @author wenhai
 * @date   2021/4/13
 */
public class SpringShutdownHookThreadDemo {
    public static void main(String[] args) throws IOException {
        GenericApplicationContext context = new GenericApplicationContext();
        context.registerShutdownHook();
        context.addApplicationListener((ApplicationListener<ContextClosedEvent>) event -> System.out.println("容器已关闭"));
        context.refresh();
        System.out.println("按任意继续");
        System.in.read();
        context.close();
    }
}
